package com.hyt.it.ogt.kq.main.controller.gov;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.common.apiInfor.DataType;
import com.hyt.common.apiInfor.ParamType;
import com.hyt.it.ogt.kq.common.gov.annotation.Progress;
import com.hyt.it.ogt.kq.common.gov.constant.ResponseCode;
import com.hyt.it.ogt.kq.main.controller.base.BaseController;
import com.hyt.it.ogt.kq.common.enums.ProgressEnum;
import com.hyt.it.ogt.kq.common.config.exception.KqException;
import com.hyt.it.ogt.kq.service.gov.model.param.MockRoomDataPageParam;
import com.hyt.it.ogt.kq.service.gov.model.vo.MockRoomDataPageVO;
import com.hyt.it.ogt.kq.service.gov.service.IRoomDataService;
import com.hyt.it.ogt.kq.service.gov.service.ITaskMockRoomDataService;
import com.hyt.model.PageParam;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * <p>
 * 模拟测试考务包表 前端控制器
 * </p>
 *
 * @author huangyh
 * @since 2022-03-17
 */
@Slf4j
@RestController
@Api(tags = "130.模拟测试考务包接口", value = "模拟测试考务包接口")
@ApiSort(value = 130)
@RequestMapping("/kw/gov/mockRoomData")
public class MockRoomDataController extends BaseController {

    @Resource
    private ITaskMockRoomDataService iTaskMockRoomDataService;

    @Resource
    private IRoomDataService iRoomDataService;

    @ApiOperation(
            value = "30.1 查询模拟测试考务包分页数据",
            notes = "43300101:查询模拟测试考务包分页数据异常"
    )
    @ApiOperationSort(value = 1)
    @GetMapping("/get/page")
    public ApiResponse<Object> page(PageParam<MockRoomDataPageVO> page,
                                    @Valid MockRoomDataPageParam param,
                                    BindingResult bindingResult) {
        log.info("# 查询模拟测试考务包分页数据参数: {}", JSON.toJSONString(param));
        try {
            //参数校验
            if (bindingResult.hasErrors()) {
                return ApiResponse.builder()
                        .code(ResponseCode.SUCCESS.getCode())
                        .data(bindingResult.getAllErrors())
                        .build();
            }
            Page<MockRoomDataPageVO> pageData = iTaskMockRoomDataService.page(page, param);
            return ApiResponse.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .data(pageData)
                    .build();
        } catch (KqException k) {
            log.error("# 查询模拟测试考务包分页数据业务异常: {}", k.getErrMsg());
            return ApiResponse.builder()
                    .code(k.getErrCode())
                    .build();
        } catch (Exception e) {
            log.error("# 查询模拟测试考务包分页数据异常", e);
            return ApiResponse.builder()
                    .code(ResponseCode.GET_TASK_MOCK_ROOM_DATA_PAGE_EXCEPTION.getCode())
                    .build();

        }
    }

    @ApiOperation(
            value = "30.2 生成考试任务下的模拟测试考务包",
            notes = "43300201:生成考试任务下的模拟测试考务包异常; " +
                    "43300202:考试任务不存在; " +
                    "43300203:未锁定编排数据，无法生成模拟测试考务包; " +
                    "43300204:模拟测试考务包已存在; " +
                    "43300205:存在未编排考生的考场，无法生成模拟测试考务包;"
    )
    @ApiOperationSort(value = 2)
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "taskId",
                    value = "考试任务id",
                    dataType = DataType.STRING,
                    paramType = ParamType.QUERY,
                    required = true
            )
    })
    @PostMapping("/create/byTask")
    @Progress(name = ProgressEnum.CREATE_MOCK_ROOM_DATA)
    public ApiResponse<Object> createByTask(@RequestParam String taskId) {
        log.info("# 生成考试任务下的模拟测试考务包参数: {}", taskId);
        try {
            iRoomDataService.createMockRoomDataByTaskId(taskId);
            return ApiResponse.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .build();
        } catch (KqException k) {
            log.error("# 生成考试任务下的模拟测试考务包业务异常: {}", k.getErrMsg());
            return ApiResponse.builder()
                    .code(k.getErrCode())
                    .data(k.getErrMsg())
                    .build();
        } catch (Exception e) {
            log.error("# 生成考试任务下的模拟测试考务包异常", e);
            return ApiResponse.builder()
                    .code(ResponseCode.CREATE_MOCK_ROOM_DATA_BY_TASK_EXCEPTION.getCode())
                    .build();
        }
    }

}
